package org.hackystat.iw.projectoverviewer.page.defaultmetrics;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.hackystat.iw.projectoverviewer.page.treemap.TreeMapPage;

/**
 * A form for setting the default metrics. The values of these metrics are passed as applet
 * parameters to the actual applet, which allows it to be refreshed yet stay with the same metrics
 * determined by the user.
 * 
 * @author Jianfei Liao
 */
public class SetMetricsForm extends Form {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  /** The metric model instance. */
  private final MetricModel metricModel;
  /** The page parameters. */
  private final PageParameters param;
  
  /** String constant for better performance. */
  private static final String STRATEGY_TOKEN = "strategy";
  private static final String COLOR_PROVIDER = "colorProvider";
  private static final String SIZE_TOKEN = "size";
  private static final String COLOR_TOKEN = "color";

  /**
   * Constructor.
   * Adds the different drop down choice components for the different metrics to this form.
   * 
   * @param id Component ID, supplied by wicket markup.
   * @param model The metric model instance.
   * @param param The page parameters.
   */
  public SetMetricsForm(String id, MetricModel model, PageParameters param) {
    super(id); 
    
    this.metricModel = model;
    this.param = param;
    
    // set the default metrics
    this.metricModel.setStrategy(new Metric("Squirified", "0"));
    this.metricModel.setColorProvider(new Metric("Red Yellow Green", "0"));
    this.metricModel.setSize(new Metric("SourceLines", "4"));
    this.metricModel.setColor(new Metric("MethodsCount", "3"));
    
    // create the drop down choices
    PropertyModel strategyModel = new PropertyModel(metricModel, STRATEGY_TOKEN);
    PropertyModel colorProviderModel = new PropertyModel(metricModel, COLOR_PROVIDER);
    PropertyModel sizeModel = new PropertyModel(metricModel, SIZE_TOKEN);
    PropertyModel colorModel = new PropertyModel(metricModel, COLOR_TOKEN);
    
    ChoiceRenderer renderer = new ChoiceRenderer("label", "value");

    DropDownChoice strategyChoices = new DropDownChoice(
                                                STRATEGY_TOKEN, 
                                                strategyModel, 
                                                new Model(MetricModel.getChoices(STRATEGY_TOKEN)), 
                                                renderer);
    DropDownChoice colorProviderChoices = new DropDownChoice(
                                                COLOR_PROVIDER, 
                                                colorProviderModel, 
                                                new Model(MetricModel.getChoices(COLOR_PROVIDER)), 
                                                renderer);
    DropDownChoice sizeChoices = new DropDownChoice(
                                                SIZE_TOKEN, 
                                                sizeModel, 
                                                new Model(MetricModel.getChoices(SIZE_TOKEN)), 
                                                renderer);
    DropDownChoice colorChoices = new DropDownChoice(
                                                COLOR_TOKEN, 
                                                colorModel, 
                                                new Model(MetricModel.getChoices(COLOR_TOKEN)), 
                                                renderer);

    // add the drop down to the form
    add(strategyChoices);
    add(colorProviderChoices);
    add(sizeChoices);
    add(colorChoices);
  }

  /**
   * Redirect to the treemap page with the selected metrics.
   */
  @Override
  protected void onSubmit() {
    param.put(STRATEGY_TOKEN, metricModel.getStrategy().getValue());
    param.put(COLOR_PROVIDER, metricModel.getColorProvider().getValue());
    param.put(SIZE_TOKEN, metricModel.getSize().getValue());
    param.put(COLOR_TOKEN, metricModel.getColor().getValue());
    
    this.setResponsePage(TreeMapPage.class, param);
  }
}